<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Matrix\Exception;

class Child extends Model
{
    use SoftDeletes;

    //
    protected $fillable = ['agency_id', 'user_id', 'name', 'birthday', 'sex', 'last_use_time', 'current', 'address', 'phone', 'age', 'agedevelop'];

    public function agency()
    {
        return $this->belongsTo(Agency::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
    /**
     * delete replace with assessments()
    */
    public function aeesessments()
    {
        return $this->hasMany(Assessment::class);
    }
    public function assessments()
    {
        return $this->hasMany(Assessment::class);
    }
    public function childPlays()
    {
        return $this->hasMany(ChildPlay::class);
    }

    public function histories()
    {
        return $this->hasMany(History::class);
    }

    public function childStars()
    {
        return $this->hasMany(ChildStar::class);
    }

    public function currentChildren()
    {
        return $this->hasMany(CurrentChild::class);
    }

    public function family()
    {
        return $this->hasOne(Family::class);
    }

    public function notices(){
        return $this->belongsToMany(Notice::class);
    }
    /*

* 拼接查询条件
*      */
    public static function selectQuery($data){
        return function ($query) use ($data) {
            if(isset($data['agency_id']) && $data['agency_id'] > 0){
                $query->where('agency_id', $data['agency_id']);
            }
            if(isset($data['user_id']) && $data['user_id'] > 0){
                $query->where('user_id', $data['user_id']);
            }
            if (isset($data['child_name']) && !empty($data['child_name'])) {
                $query->where('name', 'like',  '%' . $data['child_name'] . '%');
            }
        };
    }
}
